package cn.demo.service1.controller;

import cn.demo.service1.client.Service0Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class Service1Controller extends AbstractController {
    ////////////////////////////////////////////////////////////////////////////////
    @Value("${from}")
    private String from;
    @RequestMapping("/hello")
    String hello() {
        return this.from;
    }
    ////////////////////////////////////////////////////////////////////////////////
    @Autowired
    void setEnviroment(Environment env) {
        System.err.println("Controller1.env.from: "  + env.getProperty("from"));
    }
    ////////////////////////////////////////////////////////////////////////////////
    @Autowired
    private Environment env;
    @GetMapping("config/{configKey}")
    //@ResponseBody
    String testConfig(@PathVariable String configKey) {
        System.out.println(configKey+":"+env.getProperty(configKey));
        return env.getProperty(configKey);
    }
    ////////////////////////////////////////////////////////////////////////////////
    @Autowired
    Service0Client service0Client; // remote interface
    @GetMapping("user/{userId}/{sleepSec}")
    public String user(@PathVariable("userId") String userId, @PathVariable int sleepSec) {
        return "Appliction1.user->"+service0Client.user(userId, sleepSec);
    }
    @GetMapping("test")
    public String test() {
        Student stuName = new Student()
                .setStuName("stuName");
        User user = new User()
                .setId(0)
                .setName("asd")
                .setStudent(stuName);
        System.out.println(user.toString());
        return "Appliction1.test->"+service0Client.test(user);
    }
    /////////////////////////////////////////////////////////////////////
    public static class User {
        private int id;
        private String name;
        private Student student;
        public User() {
        }
        public int getId() {
            return id;
        }
        public User setId(int id) {
            this.id = id;
            return this;
        }
        public String getName() {
            return name;
        }
        public User setName(String name) {
            this.name = name;
            return this;
        }
        public Student getStudent() {
            return student;
        }
        public User setStudent(Student student) {
            this.student = student;
            return this;
        }
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", student=" + student +
                    '}';
        }
    }
    public static class Student {
        private String stuName;
        public Student() {
        }
        public String getStuName() {
            return stuName;
        }
        public Student setStuName(String stuName) {
            this.stuName = stuName;
            return this;
        }
        @Override
        public String toString() {
            return "Student{" +
                    "stuName='" + stuName + '\'' +
                    '}';
        }
    }
}
